<?php
/**
 * Created by PhpStorm.
 * User: iceberg
 * Date: 2018-10-21
 * Time: 14:44
 */

namespace app\system\controller;


use app\service\SignService;
use think\Controller;
use think\facade\Request;


class Sign extends Controller
{
    //签名业务
    private $signService;
    //初始化
    public function initialize()
    {
        $this->signService = new SignService();
    }

    //显示登录页面
    public function login()
    {
        return $this->fetch();
    }

    //登录检测
    public function check()
    {
        return $this->signService->check();
    }

    //登出
    public function logout()
    {
        $this->signService->logout();
        return $this->redirect('system/Sign/login');
    }

    //修改密码
    public function pwReset()
    {
        if(Request::isPost()){
            //修改密码
            //获取request参数
            $param = Request::post();
            //dump($param);die;
            if(empty($param['password'])){
                return ajax_return(100, '请填写原密码！');
            }
            if(empty($param['newpwd'])){
                return ajax_return(100, '请填写新密码！');
            }
            if(empty($param['confirmpwd'])){
                return ajax_return(100, '请填写确认密码！');
            }
            $user = \app\model\User::get(session('id'));
            //dump($user);die;
            if($user->password == md5($param['password'])){
                if($param['newpwd'] == $param['confirmpwd']){
                    $user->password = md5($param['newpwd']);
                    if($user->save()){
                        return ajax_return(0, '密码修改成功请重新登录！');
                    }else{
                        return ajax_return(100, '密码修改失败！');
                    }
                }else{
                    return ajax_return(100, '确认密码和新密码要一致！');
                }

            }else{
                return ajax_return(100, '原密码输入有误！');
            }
        }else{
            return $this->fetch();
        }
    }


}